package com.example.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.domain.GoodsOrders;
import com.example.domain.Product;
import com.example.domain.bygoods;
import com.example.service.IGoodsOrdersService;
import com.example.service.IProductService;
import com.example.tools.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 订单Controller
 *
 * @author kzc
 * @date 2023-04-22
 */
@RestController
@RequestMapping("/system/orders")
public class GoodsOrdersController
{


//@Autowired
//private GoodsOrders goodsOrders;
    @Autowired
    private IGoodsOrdersService goodsOrdersService;

    @Autowired
    private IProductService productService;

        /**
     * 查询个人订单列表
     */

    @GetMapping("/list/{userid}")
    public Result list(@PathVariable String userid)
    {
        List<GoodsOrders> gslist = goodsOrdersService.selectuserid(userid);
        if (gslist!=null)
            return Result.ok(gslist);
        else
            return
            Result.fail().message("查询失败");

    }

    @PostMapping("/order/add")
    public Result orderadd(@RequestBody bygoods goods)
    {
        if (goods!=null)
        {

            for (String str:goods.getNames()){  //遍历ids判断商品个数
                Product prodeuct = productService.selectname(str);//根据商品名来查询相关信息

//                GoodsOrders goodsOrders = new GoodsOrders();
                 GoodsOrders goodsOrders=new GoodsOrders();
                 goodsOrders.setSgoNumber(goods.getSgoNumber());
                 goodsOrders.setSpNameUrl(prodeuct.getSpNameUrl());
                goodsOrders.setUsersId(goods.getUsersId());

//                goodsOrders.setCreateTime(String.valueOf(new Date()));
                goodsOrders.setProductName(str);
                goodsOrders.setProductPrice(prodeuct.getPrice());
                goodsOrders.setAddressName(goods.getAddressName());
                goodsOrders.setUserName(goods.getUserName());
                goodsOrders.setIsdelete(0);
                goodsOrders.setProductNum(1);
                goodsOrders.setStatus(1);
                goodsOrdersService.insertGoodsOrders(goodsOrders);
                return Result.ok("成功");
            }
        }
        return  Result.fail("失败");
    }


/**
* 功能作用: 退款修改订单状态
* 描述:
* 使用规则:
* @Author: kzc
* @Date: 2023/4/26
*/

    @GetMapping("/refund/{sgoId}")
    public Result list(@PathVariable Integer sgoId)
    {
        int upsta = goodsOrdersService.upsta(sgoId);
        if (upsta!=0)
            return Result.ok("成功");
        else
            return
                    Result.fail().message("查询失败");

    }


    @GetMapping("/del/{sgoId}")
    public Result del(@PathVariable Integer sgoId)
    {
        int upsta = goodsOrdersService.deleteGoodsOrdersBySgoId(sgoId);
        if (upsta!=0)
            return Result.ok("成功");
        else
            return
                    Result.fail().message("查询失败");

    }
//

//
//    /**
//     * 导出订单列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:orders:export')")
//    @Log(title = "订单", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, GoodsOrders goodsOrders)
//    {
//        List<GoodsOrders> list = goodsOrdersService.selectGoodsOrdersList(goodsOrders);
//        ExcelUtil<GoodsOrders> util = new ExcelUtil<GoodsOrders>(GoodsOrders.class);
//        util.exportExcel(response, list, "订单数据");
//    }
//
//    /**
//     * 获取订单详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('system:orders:query')")
//    @GetMapping(value = "/{sgoId}")
//    public AjaxResult getInfo(@PathVariable("sgoId") Integer sgoId)
//    {
//        return success(goodsOrdersService.selectGoodsOrdersBySgoId(sgoId));
//    }
//
//    /**
//     * 新增订单
//     */
//    @PreAuthorize("@ss.hasPermi('system:orders:add')")
//    @Log(title = "订单", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody GoodsOrders goodsOrders)
//    {
//        return toAjax(goodsOrdersService.insertGoodsOrders(goodsOrders));
//    }
//
//    /**
//     * 修改订单
//     */
//    @PreAuthorize("@ss.hasPermi('system:orders:edit')")
//    @Log(title = "订单", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody GoodsOrders goodsOrders)
//    {
//        return toAjax(goodsOrdersService.updateGoodsOrders(goodsOrders));
//    }
//
//    /**
//     * 删除订单
//     */
//    @PreAuthorize("@ss.hasPermi('system:orders:remove')")
//    @Log(title = "订单", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{sgoIds}")
//    public AjaxResult remove(@PathVariable Integer[] sgoIds)
//    {
//        return toAjax(goodsOrdersService.deleteGoodsOrdersBySgoIds(sgoIds));
//    }
}
